# -*- coding:utf8 -*- #
# -----------------------------------------------------------------------------------
# ProjectName:  smalleel-api-tools
# FileName:     register_acceptance.py
# Author  :
# Datetime:     2023/3/28 10:59
# Description：基站和电表注册入库并出库
# -----------------------------------------------------------------------------------
from common.util.jsonUtil.jsonUtil import read_extract_json
from common.util.requestUtil.requestUtil import RequestsUtil
from tool.process.config.smalleelApi import PlatformApi
from tool.process.config.acceptConfig import AcceptanceConfig
from tool.process.acceptance.smalleel.warehouse.warehouse_acceptance import WarehouseAcceptance


class RegisterEquipment:
    def __init__(self):
        self.config = AcceptanceConfig()
        self.api = PlatformApi()
        self.req = RequestsUtil()
        self.ware = WarehouseAcceptance()
        self.platform_token = read_extract_json('tokenPlatform', 6)

    def register_collector(self, collectorNo, num):
        """
        基站注册后入库出库到对应项目
        """
        url = self.api.registerCollector
        headers = {"Token": self.platform_token}
        if self.config.deviceType == 6:
            collectorModelId = self.config.collectionModelId
        else:
            collectorModelId = self.config.collectorModelId
        params = {
            "collectorDeviceNo": collectorNo,
            "num": num,
            "collectionModelId": collectorModelId,
            "deviceType": self.config.deviceType,
            "supplierId": 4
        }
        response = self.req.send_request("post", url, params=params, headers=headers,
                                         parameterFormat="data")
        if response["code"] == 200:
            if num > 1:
                collectorStart = collectorNo[1::]
                collectorNoEnd = int(collectorStart) + int(num) - 1
                print(f"{collectorNo}-{collectorNo[0]}{collectorNoEnd}基站注册完成！")
            else:
                collectorNo = collectorNo
                print(f"{collectorNo}基站注册完成！")
        else:
            print(f"基站注册失败！失败原因: {response['msg']}")
        # 入库
        self.ware.warehouse_in(collectorModelId, collectorNo, num)
        # 出库
        self.ware.warehouse_out(collectorModelId, collectorNo, num)

    def register_meter(self, meterNo, num):
        """
        电表或水表注册后入库出库到对应项目
        """
        url = self.api.registerMeter
        headers = {"Token": self.platform_token}
        if self.config.deviceType == 6:
            deviceModelId = self.config.waterModelId
            deviceType = 4
        else:
            deviceModelId = self.config.deviceModelId
            deviceType = 2
        params = {
            "startNo": meterNo,
            "noNum": num,
            "deviceType": deviceType,
            "modelId": deviceModelId,
            "supplierId": 6
        }
        response = self.req.send_request("post", url, params=params, headers=headers,
                                         parameterFormat="data")

        if response["code"] == 200:
            print(f"{meterNo}-{meterNo + num - 1}设备（表）注册完成！")
        else:
            print(f"{meterNo}-{meterNo + num - 1}设备（表）注册失败！失败原因: {response['msg']}")
        # 入库
        self.ware.warehouse_in(deviceModelId, meterNo, num)
        # 出库
        self.ware.warehouse_out(deviceModelId, meterNo, num)


if __name__ == '__main__':
    register = RegisterEquipment()
    register.register_collector("W59712193312", 2)
